<?php
/**
 * Description of LoginDAO
 *
 * @author Magno
 */
class LoginDAO {

    private $conexao;
    private $usuarioDAO;

    public function __construct() {
        $this->conexao = Conexao::getConexao();
        $this->usuarioDAO = new UsuarioDAO();
    }

    public function salvar($login) {

        try {

            if ($login->getLoginID() != null && $login->getLoginID() > 0) {
                //nao vai ter edição
                return $login;
            } else {

                $ip = $_SERVER['REMOTE_ADDR'];

                $statment = $this->conexao->prepare("INSERT INTO login(loginID,data,ip,usuarioID)
                    VALUES (:loginID,:data,'$ip',:usuarioID);");
            }

            if ($statment->execute($login->toBD())) {

                if ($login->getLoginID() == null || $login->getLoginID() <= 0)
                    $login->setLoginID($this->conexao->lastInsertId());
                return $login;
            }else {
                $erros = $statment->errorInfo();
                throw new Exception("Erro LoginDAO: " . $erros[2]);
            }
        } catch (PDOException $err) {
            throw new Exception("Erro LoginDAO:" . $err->getMessage());
        }
    }

    public function excluir($loginID) {

        try {

            $statment = $this->conexao->prepare("DELETE FROM login WHERE loginID = :loginID");
            $statment->bindParam(':loginID', $loginID);

            if (!$statment->execute()) {
                $erros = $statment->errorInfo();
                throw new Exception("Erro LoginDAO: " . $erros[2]);
            }
        } catch (PDOException $err) {
            throw new Exception("Erro LoginDAO:" . $err->getMessage());
        }
    }

    public function listar($pagina = 0, $usuarioID = -1) {
        try {

            $limite = Util::getPaginacao($pagina);
            $where = $this->getWhere($usuarioID);
            $logins = array();
            $statment = $this->conexao->query("SELECT * FROM login u $where $limite");

            foreach ($statment->fetchAll(PDO::FETCH_SERIALIZE) as $row){
                $login = new LoginBean($row['loginID'], $row['data'], $row['ip']);
                $login->setUsuario($this->usuarioDAO->buscarPorID($row['usuarioID']));
                $logins[] =  $login;
            }

            return $logins;
        } catch (PDOException $err) {
            throw new Exception("Erro LoginDAO:" . $err->getMessage());
        }
    }

    public function listarPorOrdem($pagina = 0, $tamanho = 0, $ordem = "loginID", $direcao = "ASC") {
        try {

            $limite = Util::getPaginacao($pagina,$tamanho);
            $logins = array();
            $statment = $this->conexao->query("SELECT * FROM login u ORDER BY u.$ordem $direcao $limite");

            foreach ($statment->fetchAll(PDO::FETCH_SERIALIZE) as $row){
                $login = new LoginBean($row['loginID'], $row['data'], $row['ip']);
                $login->setUsuario($this->usuarioDAO->buscarPorID($row['usuarioID']));
                $logins[] = $login;
            }

            return $logins;
        } catch (PDOException $err) {
            throw new Exception("Erro LoginDAO:" . $err->getMessage());
        }
    }

    public function buscarPorID($loginID) {
        try {

            $row = $this->conexao->query("SELECT * FROM login WHERE loginID = $loginID ")->fetch(PDO::FETCH_SERIALIZE);

            if (isset ($row['loginID']) && $row['loginID'] > 0) {
                $login = new LoginBean($row['loginID'], $row['data'], $row['ip'], $row['ip']);
                $login->setUsuario($this->usuarioDAO->buscarPorID($row['usuarioID']));
                return $login;
            } else {
                return null;
            }
        } catch (PDOException $err) {
            throw new Exception("Erro LoginDAO:" . $err->getMessage());
        }
    }

    private function getWhere($usuarioID = -1) {

        $where = " WHERE 1 = 1 ";

        if (isset($usuarioID) && $usuarioID > 0) {
            $where = $where . " AND u.usuarioID = $usuarioID ";
        }

        return $where;
    }

    public function total($usuarioID = -1) {
        try {
            $where = $this->getWhere($usuarioID);
            $total = $this->conexao->query("SELECT COUNT(u.loginID) total FROM login u $where")->fetch(PDO::FETCH_COLUMN);
            return $total;
        } catch (PDOException $err) {
            throw new Exception("Erro LoginDAO:" . $err->getMessage());
        }
    }

}
?>